// pages/myCoupon/myCoupon.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var page = 1;
var last_page = 0;
var type;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  var couponactve = that.data.couponactve;
  if (couponactve == 0) {
    couponactve = 1
  } else if (couponactve == 1) {
    couponactve = 2
  } else if (couponactve == 2) {
    couponactve = 3
  }
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'my/voucher',
      data: { type: couponactve,page:page },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          couponList: that.data.couponList.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: ["未使用", "已失效","已使用"],
    couponList: [],
    couponactve: 0,
    queS:true
  },
  onChange(event) {
    var that = this;
    that.setData({
      queS: true
    })
    var idx = event.detail.index;
    var couponactve = that.data.couponactve;
    if (idx == couponactve) {
      return false;
    } else {
      that.setData({
        couponactve: idx,
        couponList:[]
      })
      that.getList()
    }
  },
  getList(){
    var that=this;
    var couponactve = that.data.couponactve;
    if (couponactve==0){
      couponactve=1
    } else if (couponactve==1){
      couponactve=2
    } else if (couponactve == 2) {
      couponactve = 3
    }
    if (page > 1) {
      page--;
    }

    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'my/voucher',
      data: { type: couponactve,page:page},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            couponList: res.data.data.data
          })
        } else if(res.data.code==201){
           that.setData({
             queS:false
           })
        } else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data;
         that.getList()
       },
     })
  },
  free:function(){
    wx.navigateTo({
      url: '/pages/allShops/allShops',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    page=1
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})